package com.jiangnan.ui.activity

import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.jiangnan.R
import com.jiangnan.databinding.ActivityTransactionHistoryBinding
import com.jiangnan.ui.adapter.TransactionLogAdapter
import com.jiangnan.util.SharedPrefUtil
import com.jiangnan.util.ToastUtil
import com.jiangnan.viewmodel.TransactionViewModel

class TransactionHistoryActivity : BaseActivity() {
    private lateinit var binding: ActivityTransactionHistoryBinding
    private lateinit var vm: TransactionViewModel
    private val adapter = TransactionLogAdapter()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityTransactionHistoryBinding.inflate(layoutInflater)
        setContentView(binding.root)

        vm = ViewModelProvider(this)[TransactionViewModel::class.java]
        binding.rvLogs.layoutManager = LinearLayoutManager(this)
        binding.rvLogs.adapter = adapter

        vm.logs.observe(this) { logs ->
            adapter.submit(logs)
        }
        vm.error.observe(this) { msg ->
            ToastUtil.showShort(this, msg)
        }
        vm.toastMessage.observe(this) { msg ->
            ToastUtil.showShort(this, msg)
        }

        val userId = SharedPrefUtil.getLoginInfo(this)!!.id.toLong()
        if (userId < 0) finish()
        vm.loadLogs(userId)
    }
}